<template>
  <el-dialog
    title="添加部门"
    :visible="showDialog"
    width="45%"
    @close="handleClose"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="validateFormRef" :model="formDate" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="formDate.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formDate.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formDate.manager" style="width:80%" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formDate.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="end">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button type="primary" size="small">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 导入接口
import { getDepartment } from '@/api/department'
export default {
  name: 'AddDepts',
  filters: {},
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      require: true
    },
    treeNode: {
      type: Object,
      require: true,
      default: null
    }
  },
  data() {
    // 自定义校验部门
    const validateName = (rule, value, callback) => {
      // console.log('部门名称验证')
      // console.log(rule)
      // console.log(value)
      // 返回布尔值
      // console.log(this.treeNode)
      // 判断是否有children
      if (!this.treeNode.children) return callback()
      const valid = this.treeNode.children.some(item => item.name === value)
      // console.log(valid)
      if (valid) { return callback(new Error('部门已存在')) }
      return callback()
    }
    // 自定义校验code
    const validateCode = async(rule, value, callback) => {
      const res = await getDepartment()
      const valid = res.depts.some(item => item.code === value)
      if (valid) return callback(new Error('编号重复'))
      return callback()
    }
    return {

      // 定义表单数据
      formDate: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      // 校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字', trigger: 'blur' },
          // 自定义校验规则
          { trigger: 'blur', validator: validateName }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字', trigger: 'blur' },
          { trigger: 'blur', validator: validateCode }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门编码要求1-50个字', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {
  },
  methods: {
    // 重置表单
    handleClose() {
      this.$refs.validateFormRef.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style lang='less' scoped>

</style>
